<?php
class QuoteApp extends MemberbaseApp
{
	var $_quote_mod;
    function __construct()
    {
        $this->QuoteApp();
    }
    function QuoteApp()
    {
        parent::__construct();
        $this->_quote_mod=&m('quote');
    }
    
    //报价列表
	function index(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(10);
		$conditions=$this->_get_query_conditions(array(
			array(
				'field' => 'quote_sn',
				'equal' => 'like',
				'name'	 => 'quote_sn'
			),
			array(
				'field' => 'style_sn',
				'equal' => 'like',
				'name'	 => 'style_sn'
			)
		));
		$con=$this->_get_query_conditions(array(
			array(
				'field' => 'quote_sn',
				'equal' => 'like',
				'name'	 => 'quote_sn'
			)
		));
		$order_mod=&m('order');
		$sample_mod=&m('sample');
		if ($user_info['role']==USER_MERCHANDISER_ADMIN){
			$mod_cm=&m("company_manager");
			$mod_brand=&m("brand");
			$brand_ids=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
			$quote_approve_where=' AND brand_id'.db_create_in(array_keys($brand_ids));
		}
		if ($user_info['role']==USER_FACTORY){
			$conditions.=" and quote_status=2 and FIND_IN_SET({$user_info['factory_id']},factory_quote_id)";
			$con.=" and quote_status=2 and FIND_IN_SET({$user_info['factory_id']},factory_quote_id)";
		}elseif (in_array($user_info['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			$conditions.=' and quote_status!=3';
			$con.=" and quote_status!=3";
		}else{
			$conditions.=" and merchandiser_id={$user_info['user_id']} and quote_status!=3";
			$con.=" and merchandiser_id={$user_info['user_id']} and quote_status!=3";
		}
		
		if (in_array($user_info['role'], array(USER_FACTORY,USER_MANAGER,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN))){
			$order = empty($_GET['order']) ? 'quote_time DESC' : str_replace('-', ' ', trim($_GET['order']));
			if ($order != 'quote_time DESC' || $order != 'quote_time ASC') {
				$order .= ', quote_time DESC';
			}
			$quote_list=$order_mod->find(array(
					'conditions'=>'1=1 and quote_sn is not null '.$conditions.$quote_approve_where,
					'fields'=>'order_id,quote_time,quote_sn,quote_check_date,quote_status,quantity,merchandiser,style_sn,front_pic',
					'order'=>$order,
					'count'=>true,
					'limit'=>$page['limit']
			));
			$sq_list=$sample_mod->find(array(
					'conditions'=>'1=1 and quote_sn is not null '.$con.$quote_approve_where,
					'fields'=>'quote_time,quote_sn,quote_check_date,quote_status,sample_id,front_pic,merchandiser_id,sample_sn,quantity',
					'order'=>$order,
					'count'=>true,
					'limit'=>$page['limit']
			));
			if (!empty($sq_list)){
				$member_mod=&m('member');
				foreach ($sq_list as $j=>$rows){
					$merch_id=$rows['merchandiser_id'];
					if ($merch_id){
						$member_info=$member_mod->get(array(
								'conditions'=>'user_id='.$merch_id.' and role='.USER_MERCHANDISER
						));
					}
					$sq_list[$j]['merchandiser']=$member_info['user_name'];
				}
			}
		}
		if ($user_info['role']==USER_FACTORY){
			if(!empty($quote_list)){
				foreach ($quote_list as $key=>$value){
					$order_id=$value['order_id'];
					$quote_info=$this->_quote_mod->get(array(
							'conditions'=>'order_id='.$order_id.' and factory_id='.$user_info['factory_id']
					));
					if (!empty($quote_info)){
						unset($quote_list[$key]);
					}
				}
			}
			
			if(!empty($sq_list)){
				foreach ($sq_list as $k=>$val){
					$sample_id=$val['sample_id'];
					$q_info=$this->_quote_mod->get(array(
							'conditions'=>'sample_id='.$sample_id.' and factory_id='.$user_info['factory_id']
					));
					if (!empty($q_info)){
						unset($sq_list[$k]);
					}
				}
			}
		}
		$arr=array_merge($quote_list,$sq_list);
		$order_count=$order_mod->getCount();
		$sample_count=$sample_mod->getCount();
		$page['item_count'] = $order_count+$sample_count;
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('orders', $arr);
		$this->assign('user',$user_info);
		$this->display('quote.index.html');
		$this->_curlocal(Lang::get('quote_list'));
	}
	
	//我的报价单
	function success_order(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(10);
		if ($user_info['role']==USER_FACTORY){
			$conditions=$this->_get_query_conditions(array(
					array(
							'field' => 'order_sn',
							'equal' => 'like',
							'name'  => 'order_sn'
					),
					array(
							'field' => 'style_sn',
							'equal' => 'like',
							'name'	 => 'style_sn'
					)
			));
			$conditions.=" and status ".db_create_in(array(ORDER_STATUS_FACTORY_CONFIRM)).' and quote.factory_id='.$user_info['factory_id'].' and if_option=1 and o.factory_id='.DEFAULT_FACTORY_ID;
			$quote_list=$this->_quote_mod->find(array(
					'join'=>'belongs_order',
					'conditions'=>'1=1 '.$conditions,
					'order'=>'quote_add_time desc',
					'count'=>true,
					'limit'=>$page['limit']
			));
			$page['item_count'] = $this->_quote_mod->getCount();
			$this->_format_page($page);
			$this->assign('page_info', $page);
			$this->assign('orders', $quote_list);
		}
		$this->_curlocal(Lang::get('我的报价单'));
		$this->display('quote.index.html');
	}
	
	
	//报价历史
	function history_list(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(10);
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'quote_sn',
						'equal' => 'like',
						'name'	 => 'quote_sn'
				),
				array(
						'field' => 'style_sn',
						'equal' => 'like',
						'name'	 => 'style_sn'
				)
		));
		$con=$this->_get_query_conditions(array(
				array(
						'field' => 'quote_sn',
						'equal' => 'like',
						'name'	 => 'quote_sn'
				)
		));
		$order_mod=&m('order');
		$sample_mod=&m('sample');
		if ($user_info['role']==USER_MERCHANDISER_ADMIN){
			$mod_cm=&m("company_manager");
			$mod_brand=&m("brand");
			$brand_ids=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
			$quote_approve_where=' AND brand_id'.db_create_in(array_keys($brand_ids));
		}
		if ($user_info['role']==USER_FACTORY){
			$conditions.=' and quote.factory_id='.$user_info['factory_id'].' and quote_sn is not null';
			$con.=' and quote.factory_id='.$user_info['factory_id'].' and quote_sn is not null';
		}elseif (in_array($user_info['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			$conditions.=' and quote_status=3';
			$con.=' and quote_status=3';
		}else{
			$conditions.=' and quote_status=3 and merchandiser_id='.$user_info['user_id'];
			$con.=' and quote_status=3 and merchandiser_id='.$user_info['user_id'];
		}
		
		if ($user_info['role']==USER_FACTORY){
			$quote_list=$this->_quote_mod->find(array(
					'join'=>'belongs_order',
					'fields'=>'quote.order_id,quote_time,quote_sn,quote_check_date,quote_status,quantity,merchandiser,style_sn,front_pic',
					'conditions'=>'1=1 '.$conditions,
					'order'=>'quote_add_time desc',
					'count'=>true,
					'limit'=>$page['limit']
			));
			$sq_list=$this->_quote_mod->find(array(
					'join'=>'belongs_sample',
					'fields'=>'quote_time,quote_sn,quote_check_date,quote_status,quote.sample_id,front_pic,merchandiser_id',
					'conditions'=>'1=1 '.$con,
					'order'=>'quote_add_time desc',
					'count'=>true,
					'limit'=>$page['limit']
			));
			if (!empty($sq_list)){
				$member_mod=&m('member');
				foreach ($sq_list as $j=>$rows){
					$merch_id=$rows['merchandiser_id'];
					if ($merch_id){
						$member_info=$member_mod->get(array(
								'conditions'=>'user_id='.$merch_id.' and role='.USER_MERCHANDISER
						));
					}
					$sq_list[$j]['merchandiser']=$member_info['user_name'];
				}
			}
			$quote_arr=array_merge($quote_list,$sq_list);
			$page['item_count'] = $this->_quote_mod->getCount();
		}else{
			$quote_list=$order_mod->find(array(
					'conditions'=>'1=1 and quote_sn is not null '.$conditions.$quote_approve_where,
					'fields'=>'order_id,quote_time,quote_sn,quote_check_date,quote_status,quantity,merchandiser,style_sn,front_pic',
					'order'=>$order,
					'count'=>true,
					'limit'=>$page['limit']
			));
			$sq_list=$sample_mod->find(array(
					'conditions'=>'1=1 and quote_sn is not null '.$con.$quote_approve_where,
					'fields'=>'quote_time,quote_sn,quote_check_date,quote_status,sample_id,front_pic,merchandiser_id,sample_sn',
					'order'=>$order,
					'count'=>true,
					'limit'=>$page['limit']
			));
			$quote_arr=array_merge($quote_list,$sq_list);
			$order_count=$order_mod->getCount();
			$sample_count=$sample_mod->getCount();
			$page['item_count'] = $order_count+$sample_count;
		}
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('orders', $quote_arr);
		$this->assign('user',$user_info);
		$this->_curlocal(Lang::get('我的报价历史'));
		$this->display('quote.index.html');
	}
	
	function quote_price_order(){
		$order_id=empty($_GET['order_id'])?0:intval($_GET['order_id']);
		$sample_id=isset($_GET['sample_id'])?$_GET['sample_id']:0;
		$order_mod=& m('order');
		$sample_mod=& m('sample');
		$user=$this->visitor->get();
		if (!$order_id && !$sample_id){
			$this->show_warning('order_not_exist');
			return;
		}
		if ($order_id){
			$quote_info=$this->_quote_mod->get(array(
				'conditions'=>'order_id='.$order_id.' and factory_id='.$user['factory_id']
			));
		}elseif ($sample_id){
			$quote_info=$this->_quote_mod->get(array(
				'conditions'=>'sample_id='.$sample_id.' and factory_id='.$user['factory_id']
			));
		}else{
			$quote_info=array();
		}
		
		if (!empty($quote_info)){
			$this->show_warning("您已报过该订单的价了");
			return;
		}
		if (!IS_POST){
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			$file_mod=&m('upload');
			if ($order_id){
				$order=$order_mod->get($order_id);
				//报价资料
				$order['file_quote']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_DETAIL . ' and key_id='.$order_id,
						'order' => 'file_id ASC',
				));
				//细节图
				$order['quote_thumb_img'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_THUM_IMG . ' AND key_id = ' . $order_id,
						'order' => 'file_id ASC',
				));
				//报价单
				$order['quote_price_file']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_ORDER . ' and session_id="'.session_id().'" and key_id='.DEFAULT_CUSTOMER_KEY_ID,
						'order' => 'file_id ASC',
				));
				$this->assign('order',$order);
				$this->assign('belongs_quote_order',BELONGS_QUOTE_ORDER);
			}elseif ($sample_id){
				$sample=$sample_mod->get($sample_id);
				if ($sample['merchandiser_id']){
					$member_mod=&m('member');
					$member_info=$member_mod->get(array('conditions'=>'user_id='.$sample['merchandiser_id'].' and role='.USER_MERCHANDISER));
					$sample['merchandiser']=$member_info['user_name'];
				}
				//报价资料
				$sample['file_quote']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_QUOTE_DETAIL . ' and key_id='.$sample_id,
						'order' => 'file_id ASC',
				));
				//细节图
				$sample['quote_thumb_img'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_QUOTE_THUM_IMG . ' AND key_id = ' . $sample_id,
						'order' => 'file_id ASC',
				));
				//报价单
				$sample['quote_price_file']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_ORDER . ' and session_id="'.session_id().'" and key_id='.DEFAULT_CUSTOMER_KEY_ID,
						'order' => 'file_id ASC',
				));
				$this->assign('order',$sample);
				$this->assign('belongs_quote_order',BELONGS_QUOTE_ORDER);
			}
			$this->assign('default_key_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('quote.price.html');
		}else{
			$quote_price=trim($_POST['factory_price']);
			if (empty($quote_price)){
				$this->show_warning("请填写您的报价");
				return;
			}
			$data=array(
				'quote_price'=>trim($_POST['factory_price']),
				'quote_add_time'=>gmtime(),
				'factory_id'=>$user['factory_id'],
				'order_id'	=> isset($order_id)?$order_id:0,
				'sample_id'	=> isset($sample_id)?$sample_id:0
			);
			$quote_id=$this->_quote_mod->add($data);
			if ($this->_quote_mod->has_error()){
				$this->show_warning($this->_quote_mod->get_error());
				return;
			}
			$quote_order_id=isset($_POST['quote_order_id'])?$_POST['quote_order_id']:"";
			$file_mod =& m('upload');
			if(!empty($quote_order_id))
			{
				$file_ids=implode(",",$quote_order_id);
				$cust_files_info=$file_mod->find(array(
					"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_QUOTE_ORDER." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
				));
				 
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $quote_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$quote_id,
							"session_id"=>session_id()
					));
				}
			}
			$this->show_message('报价成功','工厂报价','index.php?app=quote');
		}
	}
	
	function quote_view(){
		$order_id=empty($_GET['order_id'])?0:intval($_GET['order_id']);
		$sample_id=empty($_GET['sample_id'])?0:intval($_GET['sample_id']);
		$order_mod=& m('order');
		$sample_mod=&m('sample');
		if (!$order_id && !$sample_id){
			$this->show_warning("找不到该报价单");
			return;
		}
		$user=$this->visitor->get();
		if ($order_id){
			$quote_info=$this->_quote_mod->get(array(
					'join'=>'belongs_order',
					'conditions'=>'quote.order_id='.$order_id.' and quote.factory_id='.$user['factory_id']
			));
		}elseif ($sample_id){
			$quote_info=$this->_quote_mod->get(array(
					'join'=>'belongs_sample',
					'conditions'=>'quote.sample_id='.$sample_id.' and quote.factory_id='.$user['factory_id']
			));
			if ($quote_info['merchandiser_id']){
				$member_mod=&m('member');
				$member_info=$member_mod->get(array(
					'conditions'=>'user_id='.$quote_info['merchandiser_id'].' and role='.USER_MERCHANDISER	
				));
				$quote_info['merchandiser']=$member_info['user_name'];
			}
		}else{
			$quote_info=array();
		}
		
		
		if (!empty($quote_info)){
			$file_mod=&m('upload');
			$quote_info['quote_price_file']=$file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_QUOTE_ORDER . ' and key_id='.$quote_info['quote_id'],
					'order' => 'file_id ASC',
			));
			if ($order_id){
				//报价资料
				$quote_info['file_quote']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_DETAIL . ' and key_id='.$order_id,
						'order' => 'file_id ASC',
				));
				//细节图
				$quote_info['quote_thumb_img'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_THUM_IMG . ' AND key_id = ' . $order_id,
						'order' => 'file_id ASC',
				));
			}else{
				//报价资料
				$quote_info['file_quote']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_QUOTE_DETAIL . ' and key_id='.$sample_id,
						'order' => 'file_id ASC',
				));
				//细节图
				$quote_info['quote_thumb_img'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_QUOTE_THUM_IMG . ' AND key_id = ' . $sample_id,
						'order' => 'file_id ASC',
				));
			}
			
		}
		$style_mod =& m('style');
		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
		$this->assign('order',$quote_info);
		$this->display('quote.view.html');
	}
	
	//ajax删除工厂报价
	function drop(){
		$quote_id=empty($_GET['qid'])?0:intval($_GET['qid']);
		$arr_data=array();
		if (!$quote_id){
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('drop_error');
		}
		$this->_quote_mod->drop($quote_id);
		if ($this->_quote_mod->has_error()){
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('drop_error');
		}else{
			$arr_data['flag']=true;
			$arr_data['msg']=Lang::get('drop_success');
		}
		exit(json_encode($arr_data));
	}
	
	//ajax签订该工厂报价
	function examine(){
		$quote_id=empty($_GET['qid'])?0:intval($_GET['qid']);
		$arr_data=array();
		if (!$quote_id){
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('examine_error');
		}
		$quote_info=$this->_quote_mod->get(array(
				'join'=>'belongs_order',
				'conditions'=>'quote_id='.$quote_id
		));
		if (!empty($quote_info)){
			$this->_quote_mod->edit($quote_id,array('if_option'=>1));
		}else{
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('examine_error');
		}
		if ($this->_quote_mod->has_error()){
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('examine_error');
		}else{
			if (!empty($quote_info)){
				$quote_num=$quote_info['order_num']+$quote_info['quantity'];
				$this->_quote_mod->edit($quote_id,array('order_num'=>$quote_num));
			}
			$arr_data['order_num']=$quote_num;
			$arr_data['flag']=true;
			$arr_data['msg']=Lang::get('examine_success');
		}
		exit(json_encode($arr_data));
	}
	
	
	//ajax签订该工厂报价
	function change_examine(){
		$quote_id=empty($_GET['qid'])?0:intval($_GET['qid']);
		$arr_data=array();
		if (!$quote_id){
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('change_examine_error');
		}
		$quote_info=$this->_quote_mod->get(array(
				'join'=>'belongs_order',
				'conditions'=>'quote_id='.$quote_id
		));
		if (!empty($quote_info)){
			if ($quote_info['if_option']==1){
				$this->_quote_mod->edit($quote_id,array('if_option'=>0));
				if ($this->_quote_mod->has_error()){
					$arr_data['flag']=false;
					$arr_data['msg']=Lang::get('change_examine_error');
				}else{
					if (!empty($quote_info)){
						$quote_num=$quote_info['order_num']-$quote_info['quantity'];
						$this->_quote_mod->edit($quote_id,array('order_num'=>$quote_num));
					}
					$arr_data['order_num']=$quote_num;
					$arr_data['flag']=true;
					$arr_data['msg']=Lang::get('change_examine_success');
				}
			}
		}else{
			$arr_data['flag']=false;
			$arr_data['msg']=Lang::get('change_examine_error');
		}
		exit(json_encode($arr_data));
	}
	
	//分配工厂报价详细页
	function quote_info(){
		$user=$this->visitor->get();
		if(!in_array($user['role'], array(USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_MANAGER))){
			$this->show_warning('no_powers_to_visit');
			return;
		}
		$is_view=isset($_GET['handle'])?$_GET['handle']:'';
		$order_id=isset($_GET['order_id'])?$_GET['order_id']:0;
		$sample_id=isset($_GET['sample_id'])?$_GET['sample_id']:0;
		$order_mod=& m('order');
		$sample_mod=& m('sample');
		$member_mod=&m('member');
		if (!$order_id && !$sample_id){
			$this->show_warning("找不到该订单或样板");
			return;
		}
		if ($is_view=='show'){
			$this->assign('is_view',true);
		}
		if ($order_id){
			$order=$order_mod->get($order_id);
		}
		if ($sample_id){
			$sample=$sample_mod->get($sample_id);
		}
		if (!IS_POST){
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			
			$factory_mod =& m('factory_info');
			$this->assign('factory_list', $factory_mod->find(array('join'=>'belongs_factory_member','conditions'=>'if_show=1 and activation=1','order'=>'factory_name DESC')));
			
			if ($order_id){
				if (!$order['quote_sn']){
					if (strlen ( $order_id ) <= 6) {
						$k = 6 - strlen ( $order_id );
						for($i = 0; $i < $k; $i ++) {
							$len .= 0;
						}
						$quote_sn = 'BJ' . $len . $order_id;
					}
					$order_mod->edit($order_id,array('quote_sn'=>$quote_sn,'quote_time'=>gmtime()));
					$order['quote_sn']=$quote_sn;
					$order['quote_status']=0;
					$order['quote_time']=gmtime();
				}
				$file_mod=&m('upload');
				$order['file_quote']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_DETAIL . ' and key_id='.$order_id,
						'order' => 'file_id ASC',
				));
				//细节图
				$order['quote_thumb_img'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_QUOTE_THUM_IMG . ' AND key_id = ' . $order_id,
						'order' => 'file_id ASC',
				));
				$quote_mod=& m('quote');
				$page = $this->_get_page(10);
				//获取加工工厂的报价信息
				if (in_array($user['role'], array(USER_MANAGER,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN))){
					$quote_list=$quote_mod->find(array(
							'join'=>'belongs_factory',
							'conditions'=>'order_id='.$order_id,
							'count'=>true,
							'limit'=>$page['limit']
					));
					if (!empty($quote_list)){
						foreach ($quote_list as $key=>$rows){
							//$f_id=$rows['factory_id'];
							//$quote_list[$key]['order_num']=$quote_mod->getOne("select sum(order_num) from {$quote_mod->table} where if_option=1 and factory_id=".$f_id);
							$quote_list[$key]['file_quote']=$file_mod->find(array('conditions'=>'file_type = '.BELONGS_QUOTE_ORDER.' AND key_id = '.$rows['quote_id'],'order'=>'file_id ASC'));
						}
					}
					$page['item_count'] = $quote_mod->getCount();
					/* 报价附件 */
					$this->assign('belongs_quote_order', BELONGS_QUOTE_ORDER);
					$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
					$this->assign('quote_list',$quote_list);
				}
				$this->assign('order',$order);
				$this->assign('is_order',true);
				$this->assign('belongs_factory_quote',BELONGS_QUOTE_DETAIL);
				$this->assign('belongs_quote_thumb_img',BELONGS_QUOTE_THUM_IMG);
			}elseif ($sample_id){
				if ($sample['merchandiser_id']){
					$member_info=$member_mod->get(array(
						'conditions'=>'user_id='.$sample['merchandiser_id'].' and role='.USER_MERCHANDISER	
					));
					$sample['merchandiser']=$member_info['user_name'];
				}
				if (!$sample['quote_sn']){
					if (strlen ( $sample_id ) <= 6) {
						$k = 6 - strlen ( $sample_id );
						for($i = 0; $i < $k; $i ++) {
							$len .= 0;
						}
						$quote_sn = 'SB' . $len . $sample_id;
					}
					$sample_mod->edit($sample_id,array('quote_sn'=>$quote_sn,'quote_time'=>gmtime()));
					$sample['quote_sn']=$quote_sn;
					$sample['quote_status']=0;
					$sample['quote_time']=gmtime();
				}
				$file_mod=&m('upload');
				$sample['file_quote']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_QUOTE_DETAIL . ' and key_id='.$sample_id,
						'order' => 'file_id ASC',
				));
				//细节图
				$sample['quote_thumb_img'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_QUOTE_THUM_IMG . ' AND key_id = ' . $sample_id,
						'order' => 'file_id ASC',
				));
				$quote_mod=& m('quote');
				$page = $this->_get_page(10);
				//获取加工工厂的报价信息
				if (in_array($user['role'], array(USER_MANAGER,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN))){
					$quote_list=$quote_mod->find(array(
							'join'=>'belongs_factory',
							'conditions'=>'sample_id='.$sample_id,
							'count'=>true,
							'limit'=>$page['limit']
					));
					if (!empty($quote_list)){
						foreach ($quote_list as $key=>$rows){
							//$f_id=$rows['factory_id'];
							//$quote_list[$key]['order_num']=$quote_mod->getOne("select sum(order_num) from {$quote_mod->table} where if_option=1 and factory_id=".$f_id);
							$quote_list[$key]['file_quote']=$file_mod->find(array('conditions'=>'file_type = '.BELONGS_QUOTE_ORDER.' AND key_id = '.$rows['quote_id'],'order'=>'file_id ASC'));
						}
					}
					$page['item_count'] = $quote_mod->getCount();
					/* 报价附件 */
					$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
					$this->assign('belongs_quote_order', BELONGS_QUOTE_ORDER);
					$this->assign('quote_list',$quote_list);
				}
				$this->assign('order',$sample);
				$this->assign('belongs_factory_quote',BELONGS_SAMPLE_QUOTE_DETAIL);
				$this->assign('belongs_quote_thumb_img',BELONGS_SAMPLE_QUOTE_THUM_IMG);
			}
			$this->_format_page($page);
			$this->assign('page_info', $page);
			$this->assign('user',$user);
			$this->display('quote.info.html');
		}else{
			$sub_value=trim($_POST['sub_val']);
			if ($sub_value=="提交审批"){
				if ($user['role']==USER_MERCHANDISER){
					if ($order_id){
						if ($order['quote_status']!=2){
							$order_mod->edit($order_id,array('quote_status'=>1,'quote_check_date'=>strtotime($_POST['quote_check_date'])));
						}
					}else{
						if ($sample['quote_status']!=2){
							$sample_mod->edit($sample_id,array('quote_status'=>1,'quote_check_date'=>strtotime($_POST['quote_check_date']),'quantity'=>$_POST['quantity']));
						}
					}
				}
			}
			if ($sub_value=="审批通过"){
				$quote_arr=array();
				if (in_array($user['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN))){
					if ($order_id){
						if ($order['quote_status']!=2){
							$order_mod->edit($order_id,array('quote_status'=>2,'quote_check_date'=>strtotime($_POST['quote_check_date'])));
							$quote_arr['quote_sn']=$order['quote_sn'];
						}
						$quote_arr['factory_quote_id']=$order['factory_quote_id'];
					}else{
						if ($sample['quote_status']!=2){
							$sample_mod->edit($sample_id,array('quote_status'=>2,'quote_check_date'=>strtotime($_POST['quote_check_date']),'quantity'=>$_POST['quantity']));
							$quote_arr['quote_sn']=$sample['quote_sn'];
						}
						$quote_arr['factory_quote_id']=$sample['factory_quote_id'];
					}
					$mail = get_mail('tofactory_send_quote', array('quote' => $quote_arr));
					if ($quote_arr['factory_quote_id']){
						$merch_arr=explode(',', $quote_arr['factory_quote_id']);
						foreach($merch_arr as $item)
						{
							$member_info=$member_mod->get(array(
								'join'=>'has_factory',
								'conditions'=>'factory_info.factory_id='.$item		
							));
							if(!empty($member_info['email']))
							{
								$this->_mailto($member_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
							}
						}
					}
				}
			}
			$this->show_message('提交成功');
		}
	}
	
	
	function assign_factory(){
		$user_info=$this->visitor->get();
		$order_id=isset($_GET['order_id'])?$_GET['order_id']:0;
		$sample_id=isset($_GET['sample_id'])?$_GET['sample_id']:0;
		$order_mod=&m('order');
		$sample_mod=&m('sample');
		if (in_array($user_info['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			if (!$order_id && !$sample_id){
				$this->show_warning('order_not_exist');
				return;
			}
			if ($order_id){
				$order=$order_mod->get_info($order_id);
				if (empty($order)){
					$this->show_warning('order_not_exist');
					return;
				}
			}
			
			if ($sample_id){
				$sample=$sample_mod->get_info($sample_id);
				if (empty($sample)){
					$this->show_warning('sample_not_exist');
					return;
				}
			}
			
			if (!IS_POST){
				$factory_mod=&m('factory_info');
				$factory_list=$factory_mod->find(array(
					'join'=>'belongs_factory_member',
					'conditions'=>'activation=1 and role='.USER_FACTORY,
					'order'=>'reg_time desc'	
				));
				$this->assign('factory_list',$factory_list);
				if (!empty($order)){
					$factory_quote_id=$order['factory_quote_id'];
				}elseif (!empty($sample)){
					$factory_quote_id=$sample['factory_quote_id'];
				}else{
					$factory_quote_id=0;
				}
				$this->assign('factory_arr_id',$factory_quote_id);
				$this->display('quote.assign.html');
			}else{
				$data=array();
				$factory_arr_id=empty($_POST['factory_quote_id'])?'':$_POST['factory_quote_id'];
				$factory_quote_id=implode(',', array_filter($factory_arr_id));
				$data['factory_quote_id']=$factory_quote_id;
				if ($order_id){
					$order_mod->edit($order_id,$data);
					if ($order_mod->has_error()){
						$this->show_warning($order_mod->get_error());
						return;
					}
				}elseif ($sample_id){
					$sample_mod->edit($sample_id,$data);
					if ($sample_mod->has_error()){
						$this->show_warning($sample_mod->get_error());
						return;
					}
				}else{
					$this->show_warning('no_powers_to_visit');
					return;
				}
				$this->show_message('分派工厂报价成功');
			}
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	//停止报价
	function stop_quote(){
		$user_info=$this->visitor->get();
		$order_id=isset($_GET['order_id'])?$_GET['order_id']:0;
		$sample_id=isset($_GET['sample_id'])?$_GET['sample_id']:0;
		$order_mod=&m('order');
		$sample_mod=&m('sample');
		$result=array();
		if (in_array($user_info['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			if (!$order_id && !$sample_id){
				$this->show_warning('order_not_exist');
				return;
			}
			if ($order_id){
				$order_mod->edit($order_id,array('quote_status'=>3));
				if ($order_mod->has_error()){
					$this->show_warning($order_mod->get_error());
					return;
				}
			}
				
			if ($sample_id){
				$sample=$sample_mod->edit($sample_id,array('quote_status'=>3));
				if ($sample_mod->has_error()){
					$this->show_warning($sample_mod->get_error());
					return;
				}
			}
			$this->show_message('停止报价成功！');
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	//重置报价
	function start_quote(){
		$user_info=$this->visitor->get();
		$order_id=isset($_GET['order_id'])?$_GET['order_id']:0;
		$sample_id=isset($_GET['sample_id'])?$_GET['sample_id']:0;
		$order_mod=&m('order');
		$sample_mod=&m('sample');
		$result=array();
		if (in_array($user_info['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			if (!$order_id && !$sample_id){
				$this->show_warning('order_not_exist');
				return;
			}
			if ($order_id){
				$order_mod->edit($order_id,array('quote_status'=>0));
				if ($order_mod->has_error()){
					$this->show_warning($order_mod->get_error());
					return;
				}
			}
	
			if ($sample_id){
				$sample=$sample_mod->edit($sample_id,array('quote_status'=>0));
				if ($sample_mod->has_error()){
					$this->show_warning($sample_mod->get_error());
					return;
				}
			}
			$this->show_message('重置报价成功！');
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	//添加报价信息
	function add_quote(){
		$order_id=isset($_GET['order_id'])?intval($_GET['order_id']):0;
		$sample_id=isset($_GET['sample_id'])?intval($_GET['sample_id']):0;
		$result=array();
		if (!$order_id && !$sample_id){
			$result['msg']="网络连接错误，请稍后再试";
			$result['flag']=false;
			exit(json_encode($result));
		}
		if ($order_id){
			$order_mod=&m('order');
			$order_info=$order_mod->get($order_id);
			if (empty($order_info)){
				$result['msg']="网络连接错误，请稍后再试";
				$result['flag']=false;
				exit(json_encode($result));
			}
		}
		
		if ($sample_id){
			$sample_mod=&m('sample');
			$sample_info=$sample_mod->get($sample_id);
			if (empty($sample_info)){
				$result['msg']="网络连接错误，请稍后再试";
				$result['flag']=false;
				exit(json_encode($result));
			}
		}
		if ($_POST['quote_price']){
			$quote_data=array(
					"quote_add_time"=>gmtime(),
					"quote_price"=>trim($_POST['quote_price']),
					"factory_id"=>$_POST['member_name']
			);
			if ($sample_id){
				$quote_data['sample_id']=$sample_id;
				$q_list=$this->_quote_mod->get(array(
					'conditions'=>'sample_id='.$sample_id.' and factory_id='.$_POST['member_name']	
				));
				if(!empty($q_list)){
					$result['msg']="您已经报过价";
					$result['flag']=false;
					exit(json_encode($result));
				}
			}elseif ($order_id){
				$quote_data['order_id']=$order_id;
				$q_list=$this->_quote_mod->get(array(
						'conditions'=>'order_id='.$order_id.' and factory_id='.$_POST['member_name']
				));
				if(!empty($q_list)){
					$result['msg']="您已经报过价";
					$result['flag']=false;
					exit(json_encode($result));
				}
			}
			$q_data=array_filter($quote_data);
			if(!empty($q_data))
			{
				$quote_id=$this->_quote_mod->add($q_data);
				if ($this->_quote_mod->has_error()){
					$this->show_warning($this->_quote_mod->get_error());
					exit;
				}
			}
		}
		if ($quote_id){
			$file_mod=&m('upload');
			$quote_info=$this->_quote_mod->get(array(
					'join'=>'belongs_factory',
					'fields'=>"factory_info.factory_name,quote_price",
					'conditions'=>$quote_id
			));
			
			$quote_order_id=isset($_POST['quote_order_id'])?$_POST['quote_order_id']:"";
			if(!empty($quote_order_id))
			{
				$file_ids=implode(",",$quote_order_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_QUOTE_ORDER." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
					
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $quote_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$quote_id,
							"session_id"=>session_id()
					));
				}
			}
			if (!empty($quote_info)){
				$quote_info['quote_file']=$file_mod->get(array(
						'fields'=>'file_id,file_name,file_path',
						"conditions"=>"file_type=".BELONGS_QUOTE_ORDER." AND key_id=".$quote_id
				));
			}
			$result['flag']=true;
			$result['quote_data']=$quote_info;
			exit(json_encode($result));
		}
	}
}